<?php
	error_reporting(0);
	session_start();
	if(!session_is_registered(myusername)){
		header("location:main_login.php");
	}
	
	$dbconn = pg_connect ("host=localhost port=5432 dbname=postgres user=postgres password=leliput93");
	if (!$dbconn) {
		echo "An error occured.\n";
		exit;
	}
	$sign=false;
	extract($_POST);
	if ($submit) {
		$result = pg_query($dbconn, "select username from supermarket.akun where username = '$username'");
		$count = pg_num_rows($result);
		
		if ($count==0 & $username!="" & $password!="") {
			if (preg_match("/[^a-zA-Z0-9]/",$username)) {
				$err1="<br/><label style=color:#FF0000> username invalid!!</label>";
			}
			else {
				$result1 = pg_query($dbconn, "select no_urut_pegawai, kode_cabang from 
									  supermarket.pegawai where nama = '$nama_pegawai'");
				//$count1 = pg_num_rows($result1);
		
				$row = pg_fetch_array($result1);
				$no_urut_pegawai = $row[0];	
				$kode_cabang = $row[1];
				$result2 = pg_query($dbconn, "insert into supermarket.akun values 
								('$username','$password','$kode_cabang','$no_urut_pegawai')");
				$sign=true;
			}
		}
		
		if($username==""){
			$err1= "<label style=color:#FF0000> Anda belum memasukkan username!!</label>";
		}
		if($password==""){
			$err2= "<label style=color:#FF0000> Anda belum memasukkan password!!</label>";
		}
		
		if($count==1) {
			$err1= "<label style=color:#FF0000> username sudah ada!!</label>";
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<title>Supermarket</title>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" href="style.css" type="text/css" />
		<script type="text/javascript" src = "tgl.js"></script>
	</head>

	<body>
	    
		<div id="container" class="clearfix">
			<h1>HEMART B10<em> CABANG <?php echo " $_SESSION[namacabang]";?></em></h1>
			<h2></h2>
		     
			 <div id="content">
				<?php  if ($sign==false) { ?>
					<h3>Form Penambahan Akun</h3>
					<br/>
					<p>
					
					<?php 
						$result = pg_query($dbconn, "SELECT nama from supermarket.pegawai 
											WHERE kode_cabang = '$_SESSION[kodecabang]' and (no_urut_pegawai, kode_cabang)
											IN ((SELECT no_urut_pegawai, kode_cabang from supermarket.pegawai) 
											EXCEPT (SELECT no_urut_pegawai, kode_cabang from supermarket.akun))");
						$count = pg_num_rows($result);	
						if ($count == 0) {
							echo "Semua pegawai sudah memiliki akun. Tidak ada pegawai yang dapat dibuat akunnya.";
						}
						else if ($count > 0) {
							echo "<form name = 'formakun' method = 'post' action = 'form_akun.php'>
								Nama pegawai: <select name = 'nama_pegawai'>";
							while ( $row = pg_fetch_array($result))
								print("<option>".$row[0]."</option>");
						
							echo "</select><br/><br/>
								<label>Username	:</label> <input type = 'text' name = 'username' maxlength = '25'/>$err1<br/>
								    <label>Password	:</label> <input type = 'password' name = 'password' maxlength = '20'/>$err2<br/><br/>
								    <input type='submit' name='submit' id='submit' value='Submit' />
								    <input type='reset' name='button' id='button' value='Reset' />
								</p>
								</form>";
						}
					}
					else {
						echo "<h3>Buat Akun</h3>
							<h4>Sukses</h3>
							<br/> <p>Akun berhasil dibuat</p>";
					}
					?>
			</div>
		 
			<ul id="nav">
				<li><a href="login_success.php">Home</a></li>
				<?php 
					
					if ($_SESSION['jabatan'] == 'Admin') 
					echo "<li><a href='form_akun.php'>Buat akun</a></li>";
				?>
				<li><a href="q1.php">List Daftar Cabang</a></li>
				<?php 
					if ($_SESSION['jabatan'] == 'Manajer Operasional') {
						echo "<li><a href='pencatatan1.php'>Pencatatan Transaksi Pembelian</a></li>";
					}
				?>
				<li><a href="daftar_transaksi1.php">Daftar Transaksi Pemasok</a></li>
				<li><a href="rekapitulasi1.php">Rekapitulasi Transaksi Pembelian</a></li>
				<li><a href="produk_terlaku.php" >Daftar Produk Terlaku</a></li>
				<li><a href="B10.php" title="View B10 info">About us</a></li>
				<li><a href="logout.php">Logout</a></li>
			</ul>
		
		</div>
	 
		<address>
			&copy; 2008 All Rights Reserved. Designed by <a href="http://www.caddoo.net" title="Portfolio of Matthew Caddoo">Matthew Caddoo</a> for <a href="http://www.zymic.com" title="Zymic Free Templates">Zymic</a> <a href="http://www.zymic.com/free-templates">Free Templates</a>. <a href="http://www.zymic.com/free-web-hosting/">Free Web Hosting</a>.
		</address>

	</body>
</html>
